#include "app_task.h"

#include "app_airbag_system.h"
#include "app_all_airbag.h"
#include "app_command_process.h"
#include "app_communication.h"
#include "app_control.h"
#include "app_flank.h"
#include "app_hip.h"
#include "app_lumbar.h"
#include "app_massage.h"
#include "app_protect.h"
#include "app_sensor_process.h"
#include "app_state_feedback.h"
#include "app_storage.h"
#include "app_thigh.h"
#include "app_vent_heat.h"
#include "common_debug.h"
#include "drv_adc.h"
#include "drv_clock.h"
#include "drv_flash.h"
#include "drv_flash2eep.h"
#include "drv_gpio.h"
#include "drv_pwm.h"
#include "drv_uart.h"
#include "inf_bts7020.h"
#include "inf_cps121.h"
#include "inf_fan.h"
#include "inf_heating_pad.h"
#include "uart_drv.h"

#include <string.h>

uint8_t rx_buf[1024] = { 0 };

static void app_uart_rx_cb(uint32_t instance, void *param)
{
    uint32_t remaining = 0;
    UART_GetReceiveStatus(instance, &remaining);
    uint32_t len = sizeof(rx_buf) - remaining;
    LOG("UART RX callback, remaining: %d\n", remaining);
    if (len > 0) {
        LOG("UART RX callback, instance: %d\n", instance);
        LOG("rx_len: %d\n", len);
        for (int i = 0; i < len; ++i) {
            LOG("rx_buf[%d]: %02X\n", i, rx_buf[i]);
        }
        LOG("Rx: %.*s\n", len, rx_buf);
    }
    UART_AbortReceivingData(instance);
    UART_ReceiveData(instance, rx_buf, sizeof(rx_buf));
}

void test_func(void) { snf_drv_uart_send(UART_INSTANCE_UART0_1, (const uint8_t *)"Hello world", 11); }

scheduler_t schedu_list[SCHEDU_LIST_MAXSIZE] = {
    //    { app_airbag_system_pump_valve_coordination, 1, true, true, 0 },
    //    { app_communication_can_prepare_data, 100, true, true, 0 },
    //    { app_communication_can_send, 15, true, true, 0 },
    //    { app_communication_can_recv_handle, 50, true, true, 0 },
    //    { app_command_process_handle, 50, true, true, 0 },
    //    { app_state_feedback_handle, 100, true, true, 0 },
    //    { app_flank_handle, 50, true, true, 0 },
    //    { app_thigh_handle, 50, true, true, 0 },
    //    { app_hip_handle, 50, true, true, 0 },
    //    { app_lumbar_handle, 50, true, true, 0 },
    //    { app_massage_handle, 50, true, true, 0 },
    //    { inf_cps121_update_air_pressure, 200, true, true, 0 },
    //    { app_protect_handle, 50, true, true, 0 },
    //    //    { app_all_airbag_handle, 50, true, true, 0 },
    //    { app_vent_heat_handle, 100, true, true, 0 },
    //    { app_control_handle, 1, true, true, 0 },
    //    // { app_sensor_process_handle, 100, true, true, 0 },
    { test_func, 1000, true, true, 0 },
};

//

const uint8_t schedu_list_size = sizeof(schedu_list) / sizeof(schedu_list[0]);

/**
 * @brief 初始化任务
 *
 */
void app_task_init(void)
{
    /* System clock init : 120MHz */
    snf_drv_clock_init();

    snf_uart_config_t uart_config = {
        .instance          = UART_INSTANCE_UART0_1,
        .baud_rate         = 115200,
        .rx_callback       = app_uart_rx_cb,
        .rx_callback_param = NULL,
    };
    result_e result = snf_drv_uart_init(&uart_config);
    if (result != RESULT_STATUS_OK) {
        LOG("uart init failed\n");
    }

    snf_drv_uart_receive(UART_INSTANCE_UART0_1, rx_buf, sizeof(rx_buf));

    //    /* 初始化存储模块 */
    //    app_storage_init();

    //    /* 初始化通讯模块 */
    //    app_communication_init();

    //    /* 初始化控制模块 */
    //    app_control_init();

    //    /* 初始化按摩模块 */
    //    app_massage_init();

    //    /* 初始化腰托模块 */
    //    app_lumbar_init();

    //    /* 初始化侧翼模块 */
    //    app_flank_init();

    //    /* 初始化腿部模块 */
    //    app_thigh_init();

    //    /* 初始化臀部模块 */
    //    app_hip_init();

    //    /* 初始化命令处理模块 */
    //    app_command_process_init();

    //    /* 初始化保护模块 */
    //    app_protect_init();

    //    /* 初始化气压传感器 */
    //    inf_cps121_init();

    //    /* 初始化通风加热模块 */
    //    app_vent_heat_init();

    /* 初始化全气袋系统 */
    //    app_all_airbag_init();

    LOG("app task init done\n");
}
